package com.qf.lanzhou.blog.api.dao.Impl;

import com.qf.lanzhou.blog.api.dao.TagDao;
import com.qf.lanzhou.blog.api.pojo.entity.Tag;
import com.qf.lanzhou.blog.api.util.BeanProcessor;
import com.qf.lanzhou.blog.api.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Nerly
 * @Date 2020/12/16 15:23
 * @JDK 1.8
 * @Version 1.0
 */
public class TagDaoImpl implements TagDao {
    private static QueryRunner queryRunner;

    static {
        queryRunner = new QueryRunner(DruidUtil.getDataSource());
    }

    @Override
    public Tag getTagByName(Tag tag) {
        String sql = "SELECT * from t_tags where tag_name=?";
        BeanHandler<Tag> handler = BeanProcessor.getBeanHandler(Tag.class);
        Tag resultTag = null;
        try {
            resultTag = queryRunner.query(sql, handler, tag.getTagName());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultTag;
    }

    @Override
    public Tag addTag(Tag tag) {
        String sql = "INSERT INTO t_tags(tag_name,tag_desc,tag_type) VALUES(?,?,?);";
        System.out.println("addDao:" + tag);
        Object[] parm = {tag.getTagName(), tag.getTagDesc(), tag.getTagType()};
        int result = 0;
        try {
            result = queryRunner.update(sql, parm);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (result == 0) {
            return null;
        }
        return tag;
    }

    @Override
    public List<Tag> selectTag() {
        String sql = "select * from t_tags";
        BeanListHandler<Tag> beanListHandler = BeanProcessor.getBeanListHandler(Tag.class);
        try {
            List<Tag> list = queryRunner.query(sql, beanListHandler);
            System.out.println("tagDao:" + list.toString());
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void deleteTag(Tag tag) {
        String sql = "DELETE FROM t_tags WHERE tag_id=?";
        BeanHandler beanHandler = BeanProcessor.getBeanHandler(Tag.class);
        try {
            queryRunner.update(sql, tag.getTagId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateTag(Tag tag) {
        String sql = "UPDATE t_tags SET tag_name=?,tag_desc=?,tag_type=?  WHERE tag_id=?";
        Object[] params = {tag.getTagName(), tag.getTagDesc(), tag.getTagType(), tag.getTagId()};
        BeanHandler beanHandler = BeanProcessor.getBeanHandler(Tag.class);
        try {
            queryRunner.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Tag> checkArticleTagByArticleId(int articleId) {
        String sql="SELECT  tag.article_id,b.tag_name,b.tag_desc,b.tag_type FROM t_article  tag " +
                "INNER JOIN  r_article_tag  a ON tag.article_id=a.article_id INNER JOIN t_tags  b ON a.tag_id=b.tag_id " +
                "WHERE tag.article_id= ? GROUP BY b.tag_id;";
        BeanListHandler beanListHandler = BeanProcessor.getBeanListHandler(Tag.class);
        List query=null;
        try {
            query = (List) queryRunner.query(sql, beanListHandler, articleId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }
}
